package cn.asterly.cicada.web.controller;



import cn.asterly.cicada.common.model.DataTablesResult;
import cn.asterly.cicada.common.model.Result;
import cn.asterly.cicada.common.model.ResultMessge;
import cn.asterly.cicada.common.model.ResultStatus;
import cn.asterly.cicada.common.utils.RandomName;
import cn.asterly.cicada.model.User;
import cn.asterly.cicada.service.base.IBaseService;
import cn.asterly.cicada.service.impl.UserServiceImpl;
import cn.asterly.cicada.web.servicebase.ServiceBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/*
 * @Description  用户信息接口类
 * @Date 22:27 2019-02-20
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/user")
@Api(value = "userController",description = "用户的处理信息")
public class UserController extends ServiceBean {
    private static Logger logger=Logger.getRootLogger();

//    @Autowired
//    public UserServiceImpl userService;

    /**
     * @Description  添加用户
     * @Date 22:28 2019-02-20
     * @Param [] 传入参数为对象
     * @return cn.asterly.cicada.common.model.Result
     **/
    @ApiOperation(value = "添加用户",httpMethod="POST")
    @RequestMapping(value = "/adduser",method = RequestMethod.POST)
    public Result adduser(User user){


        //针对用户密码再次进行MD5摘要加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //为用户随机生成一个用户名
        user.setId(UUID.randomUUID().toString());
        if(user.getName()==null&&user.getSex()==null){
            String s = RandomName.randomName();
            System.out.println("---------------------------------------------------------------------------------------------\n "+s);
            user.setName("id_"+s);
            user.setSex("男");
            user.setRegtime(new Date());
        }

        //ִ执行操作
        if(userService.insert(user)){
            return ResultStatus.addsuccess();
        }

        return ResultStatus.adderror();
    }

    /**
     * @Description  修改用户信息
     * @Date 23:07 2019-02-20
     * @Param [user]
     * @return cn.asterly.cicada.common.model.Result
     **/
    @ApiOperation(value = "修改用户信息",httpMethod="POST")
    @RequestMapping(value = "/edituser")
    public Result editUser(User user){
        if(userService.updateByPrimaryKeySelective(user)){
            return ResultStatus.addsuccess();
        }
        return ResultStatus.adderror();
    }

    @ApiOperation(value = "根据id删除用户",httpMethod="DELETE")
    @RequestMapping(value = "/delbyid/{id}")
    public Result delByID(String id){
        if(userService.deleteByPrimaryKey(id)){
            return ResultStatus.delsuccess();
        }
        return ResultStatus.delerror();
    }

    @RequestMapping(value = "/selbyid/{id}")
    @ApiOperation(value = "根据id查询指定用户信息",httpMethod="GET")
    public Result selByID(String id){

        User usr = userService.selectByPrimaryKey(id);
        return new Result(true, "",5200,usr);
    }



}
